/*
04-树5 Root of AVL Tree
*/
#include <stdio.h>
#include <stdlib.h>

/* Tree ==================================*/
#ifndef TreeElementType
#define TreeElementType int
#endif

#ifndef OutTreeElement
#define OutTreeElement(e) printf("%d", e);
#endif

typedef struct TreeNode *Tree;
typedef struct TreeNode TreeNode;
struct TreeNode {
    TreeElementType data;
    TreeNode *left;
    TreeNode *right;
};

TreeNode* createTreeNode(TreeElementType data) {
    TreeNode* node = calloc(1, sizeof(TreeNode));
    node->data = data;
    return node;
}

void freeTree(TreeNode* root) {
    if(root->left) {
        freeTree(root->left);
    }
    if(root->right) {
        freeTree(root->right);
    }
    free(root);
}

/* Tree AVL =============================================*/

int max(int a, int b) {
    return a > b ? a : b;
}

// 计算树高度
int getHeight(TreeNode* node) {
    if (node == NULL) {
        return 0;
    }
    int leftHeight = getHeight(node->left);
    int rightHeight = getHeight(node->right);
    return max(leftHeight, rightHeight) + 1;
}

TreeNode* RR(TreeNode* A) {
    // 新结点插入在A(Root)的右孩子B的右子树中
    TreeNode* B = A->right;
    A->right = B->left;
    B->left = A;
    return B;
}

TreeNode* LL(TreeNode* A) {
    // 新结点插入在A的左孩子B的左子树中
    TreeNode* B = A->left;
    A->left = B->right;
    B->right = A;
    return B;
}

TreeNode* LR(TreeNode* A) {
    // 新结点插在A的左子树B的右子树C中
    TreeNode* B = A->left;
    TreeNode* C = B->right;
    B->right = C->left;
    A->left = C->right;
    C->left = B;
    C->right = A;
    return C;
}

TreeNode* RL(TreeNode* A) {
    // 新结点插入在A(Root)的右孩子B的左子树中
    TreeNode* B = A->right;
    TreeNode* C = B->left;
    A->right = C->left;
    B->left = C->right;
    C->left = A;
    C->right = B;
    return C;
}

Tree insertTree(TreeNode* root, TreeElementType x) {
    if (root == NULL) {
        root = (TreeNode*) malloc(sizeof(TreeNode));
        root->data = x;
        root->left = root->right = NULL;
        return root;
    }
    if (x < root->data) {
        root->left = insertTree(root->left, x);
        if (getHeight(root->left) - getHeight(root->right) > 1) {
            if (x < root->left->data) {
                // printf("LL %d\n", root->data);
                root = LL(root);
            } else {
                // printf("LR %d\n", root->data);
                root = LR(root);
            }
        }
    } else  {
        root->right = insertTree(root->right, x);
        if (getHeight(root->right) - getHeight(root->left) > 1) {
            if (x > root->right->data) {
                // printf("RR %d\n", root->data);
                root = RR(root);
            } else {
                // printf("RL %d\n", root->data);
                root = RL(root);
            }
        }
    }
    return root;
}

Tree createTree(int n) {
    TreeNode* root = NULL;
    for (int i=0; i<n; i++) {
        int v;
        scanf("%d", &v);
        root = insertTree(root, v);
    }
    return root;
}

int main() {
    int N;
    scanf("%d", &N);
    Tree tree = createTree(N);
    // inorderTraversal(tree);
    // printf("\n");

    printf("%d\n", tree->data);
    freeTree(tree);
    return 0;
}
